package ru.vsu.cs.pm.pages;

import org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator;
import org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.wicketstuff.annotation.mount.MountPath;
import ru.vsu.cs.pm.BasePage;
import ru.vsu.cs.pm.PMSession;
import ru.vsu.cs.pm.beans.DealerShipManager;
import ru.vsu.cs.pm.models.*;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * User: ohodyakov
 * Date: 05.01.2011
 * Time: 16:41:03
 */
@MountPath(path = "transfers")
public class MoneyTransferListPage extends BasePage {

    @SpringBean
    private DealerShipManager dealerShipManager;

    private static final int ROWS_PER_PAGE = 25;

    public MoneyTransferListPage() {
        IColumn[] columns = new IColumn[] {
            new PropertyColumn<DealerShip>(Model.of("Account From"), "accountFrom.title"),
            new PropertyColumn<DealerShip>(Model.of("Account To"), "accountTo.title"),
            new PropertyColumn<DealerShip>(Model.of("Description"), "description"),
            new PropertyColumn<DealerShip>(Model.of("Money"), "money"),
            new AbstractColumn<DealerShip>(Model.of("Actions")) {
                @Override
                public void populateItem(Item<ICellPopulator<DealerShip>> cell, String wicketId, IModel<DealerShip> model) {
                    cell.add(new ActionPanel(wicketId, model));
                }
            }
        };

        User user = PMSession.get().getAuthenticatedUser();
        SortableDataProvider<DealerShip> dataProvider = new MoneyTransfersDataProvider(user);
        DefaultDataTable transfersTable = new DefaultDataTable("transfersTable", columns, dataProvider, ROWS_PER_PAGE);
        add(transfersTable);

        add(new Link("addTransferLink") {
            @Override
            public void onClick() {
                setRedirect(true);
                setResponsePage(MoneyTransferPage.class);
            }
        });
        add(new Link("addIncomeLink") {
            @Override
            public void onClick() {
                setRedirect(true);
                DealerShipPage page = new DealerShipPage(ItemType.INCOME);
                setResponsePage(page);
            }
        });
        add(new Link("addExpenseLink") {
            @Override
            public void onClick() {
                setRedirect(true);
                DealerShipPage page = new DealerShipPage(ItemType.EXPENSE);
                setResponsePage(page);
            }
        });
    }

    private class MoneyTransfersDataProvider extends SortableDataProvider<DealerShip> {

        private User user;

        public MoneyTransfersDataProvider(User user) {
            this.user = user;
        }

        @Override
        public Iterator<? extends DealerShip> iterator(int start, int count) {
            List<ItemType> types = Arrays.asList(ItemType.INCOME, ItemType.EXPENSE, ItemType.TRANSFER, ItemType.RESERVE);
            return dealerShipManager.getDealerShipsForUserByType(user, types, start, count).iterator();
        }

        @Override
        public int size() {
            List<ItemType> types = Arrays.asList(ItemType.INCOME, ItemType.EXPENSE, ItemType.TRANSFER, ItemType.RESERVE);
            return dealerShipManager.getNumberOfDealerShipsForUserByType(user, types);
        }

        @Override
        public IModel<DealerShip> model(DealerShip dealerShip) {
            return new DealerShipModel(dealerShip);
        }
    }

    private class ActionPanel extends Panel {

        public ActionPanel(String wicketId, final IModel<DealerShip> dealerShipIModel) {
            super(wicketId);

            add(new Link("editAction") {
                @Override
                public void onClick() {
                    setRedirect(true);
                    setResponsePage(new MoneyTransferPage(dealerShipIModel));
                }
            });

            add(new Link("removeAction") {
                @Override
                public void onClick() {
                    // TODO implement
                }
            });
        }
    }
}